<?php

class Product extends Model
{

    public $total;

    public function tableName()
    {
        return 'product';
    }

    public function rules()
    {
        return array(
            array(
                'contract_id, form, serial, unit, product_name, product_page, bowel, size, order',
                'required'
            ),
            array(
                'contract_id, quantity, product_page, color, signed, sohuy, banhuy, quantity_t, quantity_d, hoavan, logo, matma, order',
                'numerical',
                'integerOnly' => true
            ),
            array(
                'form, serial, unit, product_name, bowel, size, price',
                'length',
                'max' => 255
            ),
            array(
                'product_id, contract_id, form, serial, quantity, unit, product_name, product_page, price, color, bowel, size, signed, sohuy, banhuy, quantity_t, quantity_d, hoavan, logo, matma, order',
                'safe',
                'on' => 'search'
            )
        );
    }

    public function relations()
    {
        return array();
    }

    public function attributeLabels()
    {
        return array(
            'product_id' => 'Product',
            'contract_id' => 'Contract',
            'form' => 'Form',
            'serial' => 'Serial',
            'quantity' => 'Quantity',
            'unit' => 'Unit',
            'product_name' => 'Product Name',
            'product_page' => 'Product Page',
            'price' => 'Price',
            'color' => 'Color',
            'bowel' => 'Bowel',
            'size' => 'Size',
            'signed' => 'Signed',
            'sohuy' => 'Sohuy',
            'banhuy' => 'Banhuy',
            'quantity_t' => 'Quantity T',
            'quantity_d' => 'Quantity D',
            'hoavan' => 'Hoavan',
            'logo' => 'Logo',
            'matma' => 'Matma',
            'order' => 'Order'
        );
    }

    public function search()
    {
        $criteria = new CDbCriteria;
        $criteria->compare('product_id', $this->product_id);
        $criteria->compare('contract_id', $this->contract_id);
        $criteria->compare('form', $this->form, true);
        $criteria->compare('serial', $this->serial, true);
        $criteria->compare('quantity', $this->quantity);
        $criteria->compare('unit', $this->unit, true);
        $criteria->compare('product_name', $this->product_name, true);
        $criteria->compare('product_page', $this->product_page);
        $criteria->compare('price', $this->price);
        $criteria->compare('color', $this->color);
        $criteria->compare('bowel', $this->bowel, true);
        $criteria->compare('size', $this->size, true);
        $criteria->compare('signed', $this->signed);
        $criteria->compare('sohuy', $this->sohuy);
        $criteria->compare('banhuy', $this->banhuy);
        $criteria->compare('quantity_t', $this->quantity_t);
        $criteria->compare('quantity_d', $this->quantity_d);
        $criteria->compare('hoavan', $this->hoavan);
        $criteria->compare('logo', $this->logo);
        $criteria->compare('matma', $this->matma);
        $criteria->compare('order', $this->order);
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria
        ));
    }

    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    protected function beforeSave()
    {
        $this->price = implode("", explode(",", $this->price));
        if (empty($this->quantity)) {
            $this->quantity = $this->getQuantity($this->quantity_t, $this->quantity_d, $this->unit);
        }
        return parent::beforeSave();
    }

    public function getQuantity($qty_t, $qty_d, $unit)
    {
        if ($unit == 'quyen50') {
            if (($qty_t > $qty_d) || ($qty_t % 50 != 1) || ($qty_d % 50 != 0)) {
                return false;
            } else {
                return (($qty_d - $qty_t + 1) / 50);
            }
        } elseif ($unit == 'quyen100') {
            if (($qty_t > $qty_d) || ($qty_t % 100 != 1) || ($qty_d % 100 != 0)) {
                return false;
            } else {
                return (($qty_d - $qty_t + 1) / 100);
            }
        } else {
            if ($qty_t > $qty_d) {
                return false;
            } else {
                return ($qty_d - $qty_t + 1);
            }
        }
    }

    public function cancelNumber($p_page, $qty_t, $qty_d)
    {
        $result = round(((($qty_d - $qty_t) + 1) * $p_page) / 40);
        return $result;
    }

    public function cancelFilm($p_page, $color)
    {
        $result = $p_page + $color - 1;
        return $result;
    }

    public function getQuantity_d($unit, $qty)
    {
        switch ($unit) {
            case 'quyen50':
                return $qty * 50;
                break;
            case 'quyen100':
                return $qty * 100;
                break;
            case 'so':
                return $qty;
                break;
            case 'thung':
                return $qty * 500;
                break;
            default:
                return '';
                break;
        }
    }

    protected function afterFind(){
        parent::afterFind();
        $this->total = $this->price * $this->quantity;
    }
}